package top.zhimingw.tankgame4;

import java.util.Vector;

/**
 * @Author:zhimingw
 * @Version 1.0
 */
@SuppressWarnings("all")
public class EnemyTank extends Tank implements Runnable{
    //保存Shot
    Vector<Shot> shots = new Vector<>();
    boolean isLive = true;
    //存放敌人的坦克集合
    Vector<EnemyTank> enemyTanks = new Vector<>();
    public EnemyTank(int x,int y){
        super(x,y);
    }

    public Vector<EnemyTank> getEnemyTanks() {
        return enemyTanks;
    }

    public void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        this.enemyTanks = enemyTanks;
    }
    //判断当前敌人坦克是否和其他坦克发生重叠
    public boolean isTouchEnemyTank(){
        switch (this.getDirect()){
            case 0:
                //本身方向向上，取出一个其他敌人坦克
                for(int i=0;i<enemyTanks.size();i++){
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不和自己比较
                    if(enemyTank!=this){
                        //如果其他敌人是上下
                        if(enemyTank.getDirect()==0||enemyTank.getDirect()==2){
                            if(this.getX()>=enemyTank.getX()
                                    &&this.getX()<=enemyTank.getX()+40
                                    &&this.getY()>=enemyTank.getY()
                                    &&this.getY()<=enemyTank.getY()+60)
                                return true;
                            if(this.getX()+40>=enemyTank.getX()
                                    &&this.getX()+40<=enemyTank.getX()+40
                                    &&this.getY()>=enemyTank.getY()
                                    &&this.getY()<=enemyTank.getY()+60)
                                return true;
                        }
                        //如果其他敌人是左右
                        if(enemyTank.getDirect()==1||enemyTank.getDirect()==3){
                            if(this.getX()>=enemyTank.getX()
                                    &&this.getX()<=enemyTank.getX()+60
                                    &&this.getY()>=enemyTank.getY()
                                    &&this.getY()<=enemyTank.getY()+40)
                                return true;
                            if(this.getX()+40>=enemyTank.getX()
                                    &&this.getX()+40<=enemyTank.getX()+60
                                    &&this.getY()>=enemyTank.getY()
                                    &&this.getY()<=enemyTank.getY()+40)
                                return true;
                        }

                    }
                }
                break;
            case 1:
                //本身方向向右，取出一个其他敌人坦克
                for(int i=0;i<enemyTanks.size();i++){
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不和自己比较
                    if(enemyTank!=this){
                        //如果其他敌人是上下
                        if(enemyTank.getDirect()==0||enemyTank.getDirect()==2){
                            if(this.getX()+60>=enemyTank.getX()
                                    &&this.getX()+60<=enemyTank.getX()+40
                                    &&this.getY()>=enemyTank.getY()
                                    &&this.getY()<=enemyTank.getY()+60)
                                return true;
                            if(this.getX()+60>=enemyTank.getX()
                                    &&this.getX()+60<=enemyTank.getX()+40
                                    &&this.getY()+40>=enemyTank.getY()
                                    &&this.getY()+40<=enemyTank.getY()+60)
                                return true;
                        }
                        //如果其他敌人是左右
                        if(enemyTank.getDirect()==1||enemyTank.getDirect()==3){
                            if(this.getX()+60>=enemyTank.getX()
                                    &&this.getX()+60<=enemyTank.getX()+60
                                    &&this.getY()>=enemyTank.getY()
                                    &&this.getY()<=enemyTank.getY()+40)
                                return true;
                            if(this.getX()+60>=enemyTank.getX()
                                    &&this.getX()+60<=enemyTank.getX()+60
                                    &&this.getY()+40>=enemyTank.getY()
                                    &&this.getY()+40<=enemyTank.getY()+40)
                                return true;
                        }
                    }
                }
                break;
            case 2:
                //本身方向向下，取出一个其他敌人坦克
                for(int i=0;i<enemyTanks.size();i++){
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不和自己比较
                    if(enemyTank!=this){
                        //如果其他敌人是上下
                        if(enemyTank.getDirect()==0||enemyTank.getDirect()==2){
                            if(this.getX()>=enemyTank.getX()
                                    &&this.getX()<=enemyTank.getX()+40
                                    &&this.getY()+60>=enemyTank.getY()
                                    &&this.getY()+60<=enemyTank.getY()+60
                            )
                                return true;
                            if(this.getX()+40>=enemyTank.getX()
                                    &&this.getX()+40<=enemyTank.getX()+40
                                    &&this.getY()+60>=enemyTank.getY()
                                    &&this.getY()+60<=enemyTank.getY()+60)
                                return true;
                        }
                        //如果其他敌人是左右
                        if(enemyTank.getDirect()==1||enemyTank.getDirect()==3){
                            if(this.getX()>=enemyTank.getX()
                                    &&this.getX()<=enemyTank.getX()+60
                                    &&this.getY()+60>=enemyTank.getY()
                                    &&this.getY()+60<=enemyTank.getY()+40)
                                return true;
                            if(this.getX()+40>=enemyTank.getX()
                                    &&this.getX()+40<=enemyTank.getX()+60
                                    &&this.getY()+60>=enemyTank.getY()
                                    &&this.getY()+60<=enemyTank.getY()+40)
                                return true;
                        }

                    }
                }
                break;
            case 3:
                //本身方向向左，取出一个其他敌人坦克
                for(int i=0;i<enemyTanks.size();i++){
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不和自己比较
                    if(enemyTank!=this){
                        //如果其他敌人是上下
                        if(enemyTank.getDirect()==0||enemyTank.getDirect()==2){
                            if(this.getX()>=enemyTank.getX()
                                    &&this.getX()<=enemyTank.getX()+40
                                    &&this.getY()>=enemyTank.getY()
                                    &&this.getY()<=enemyTank.getY()+60)
                                return true;
                            if(this.getX()>=enemyTank.getX()
                                    &&this.getX()<=enemyTank.getX()+40
                                    &&this.getY()+40>=enemyTank.getY()
                                    &&this.getY()+40<=enemyTank.getY()+60)
                                return true;
                        }
                        //如果其他敌人是左右
                        if(enemyTank.getDirect()==1||enemyTank.getDirect()==3){
                            if(this.getX()>=enemyTank.getX()
                                    &&this.getX()<=enemyTank.getX()+60
                                    &&this.getY()>=enemyTank.getY()
                                    &&this.getY()<=enemyTank.getY()+40)
                                return true;
                            if(this.getX()>=enemyTank.getX()
                                    &&this.getX()<=enemyTank.getX()+60
                                    &&this.getY()+40>=enemyTank.getY()
                                    &&this.getY()+40<=enemyTank.getY()+40)
                                return true;
                        }

                    }
                }
                break;
        }
        return false;
    }

    @Override
    public void run() {
        while(true){
            if(isLive&&shots.size()<5){
                Shot s = null;
                switch (getDirect()){
                    case 0:
                        s = new Shot(getX()+20,getY(),0);
                        break;
                    case 1:
                        s = new Shot(getX()+60,getY()+20,1);
                        break;
                    case 2:
                        s = new Shot(getX()+20,getY()+60,2);
                        break;
                    case 3:
                        s = new Shot(getX(),getY()+20,3);
                        break;
                }
                shots.add(s);
                new Thread(s).start();
            }

            //根据坦克方向来继续移动
            switch (getDirect()){
                case 0:
                    for(int i=0;i<20;i++){
                        if(getY()>0&&!isTouchEnemyTank()){
                            moveUp();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1:
                    for(int i=0;i<20;i++){
                        if(getX()<940&&!isTouchEnemyTank()){
                            moveRight();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2:
                    for(int i=0;i<20;i++){
                        if(getY()<690&&!isTouchEnemyTank()){
                            moveDown();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3:
                    for(int i=0;i<20;i++){
                        if(getX()>0&&!isTouchEnemyTank()){
                            moveLeft();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }

            //然后随机的改变坦克的方向
            setDirect((int)(Math.random()*4));
            //什么时候终止线程
            if(!isLive){
                break;
            }
        }
    }
}
